package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.teacher.adapter.BirthdayGvAdapter;
import com.ycsj.goldmedalnewconcept.teacher.bean.BirthdayInfo;
import com.ycsj.goldmedalnewconcept.teacher.bean.StudentBirthdayInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 生日祝福
 * Created by Administrator on 2018/7/4 0004.
 */

public class TeacherBirthdayActivity extends BaseActivity implements View.OnClickListener {


    private ImageView ivBack;

    //标题
    private TextView tvTitle;

    private SmartRefreshLayout srf;
    //列表
    private RecyclerView rv;
    private Button btSend;
    private LinearLayout rlContent;

    //加载布局
    private RelativeLayout rlLoading;

    //适配器
    private BirthdayGvAdapter adapter;

    //空视图
    private View emptyView;

    //数据集合
    private List<BirthdayInfo.ListBean> oldDatas;

    //当前页面关闭的标记
    public static final int CLOSETYPE = 0x430;

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_birthday;
    }

    @Override
    protected void init() {

        initStateBar();

        //初始化视图
        initView();

        //初始化下拉刷新
        initSrf();

        //初始化列表
        initRv();

        //请求数据
        http(BqaManager.NORML);

    }

    /**
     * 初始化下拉刷新
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, null);
    }

    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        srf = findViewById(R.id.srf);
        rv = findViewById(R.id.rv);
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("生日祝福");
        btSend = findViewById(R.id.bt_send);
        btSend.setOnClickListener(this);
        rlContent = findViewById(R.id.rl_content);
        rlLoading = findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
        rlContent.setVisibility(View.GONE);
    }

    /**
     * 初始化列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        TextView tvMessage = emptyView.findViewById(R.id.tv_content);
        tvMessage.setText("近期没有要过生日的学生");
        tvMessage.setTextColor(getResources().getColor(R.color.coffee));
        adapter = new BirthdayGvAdapter(this, R.layout.teacher_rv_birthday, oldDatas);
    }

    /**
     * 请求页面数据
     */
    private void http(final int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "38");
        map.put("acc", AccountUtils.getAccount(this));
        HttpUtils.post(HttpAddress.BIRTHDAY, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                ToastUtils.showShort(BaseApplication.ERROR);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                if (rlContent != null && rlContent.getVisibility() != View.VISIBLE) {
                    rlContent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {
                    BirthdayInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, BirthdayInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }

                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            List<BirthdayInfo.ListBean> newDatas = info.getList();
                            if (newDatas != null) {
                                if (method == BqaManager.NORML) {
                                    oldDatas.clear();
                                    oldDatas.addAll(newDatas);
                                    for (int i = 0; i < oldDatas.size(); i++) {
                                        oldDatas.get(i).setSelected(true);
                                    }
                                    GridLayoutManager layoutManager = new GridLayoutManager(TeacherBirthdayActivity.this, 4);
                                    BqaManager.setRv(emptyView, layoutManager, TeacherBirthdayActivity.this, adapter, rv, null);
                                }
                            }
                        } else {
                            ToastUtils.showShort(Constants.ERRORSTATE + info.getState());
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.bt_send) {
            //送上祝福
            doSendAction();
        }
    }

    /**
     * 送上祝福
     */
    private void doSendAction() {
        if (oldDatas.size() != 0) {
            //证明没有学生过生日
            if (hasSelectStudent()) {
                //已经选择了学生,跳转到生日祝福界面
                //将所有选中的学生的数据携带给下一个界面
                List<StudentBirthdayInfo> infos = new ArrayList<>();
                for (int i = 0; i < oldDatas.size(); i++) {
                    if (oldDatas.get(i).isSelected()) {
                        StudentBirthdayInfo info = new StudentBirthdayInfo();
                        info.setBirthday(oldDatas.get(i).getBirthday());
                        info.setStudent(oldDatas.get(i).getUserid());
                        infos.add(info);
                    }
                }

                Intent intent = new Intent(this, TeacherSendBirthdayWishActivity.class);
                intent.putExtra("data", GsonUtil.listToJson(infos));
                startActivity(intent);

            } else {
                //未选择学生
                ToastUtils.showShort("请先选择接收祝福的学生");
                return;
            }
        } else {
            ToastUtils.showShort("该月没有学生过生,不需要送祝福");
        }

    }


    /**
     * 判断是否有选中学生的方法
     */
    private boolean hasSelectStudent() {
        boolean res = false;
        if (null != oldDatas && oldDatas.size() != 0) {
            for (int i = 0; i < oldDatas.size(); i++) {
                if (oldDatas.get(i).isSelected() == true) {
                    res = true;
                    break;
                }

            }
        }
        return res;
    }

    /**
     * 处理事件
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        if (busInfo != null && busInfo.type == CLOSETYPE) {
            finish();
        }
    }

    /**
     * 释放适配器的Handler对象
     */
    @Override
    protected void onDestroy() {
        if (null != adapter) {
            adapter.releaseHandler();
        }
        super.onDestroy();
    }
}
